<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物流产品">
              <a-input placeholder="请输入物流产品" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物流产品代码">
              <a-input placeholder="请输入物流产品代码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物流渠道">
              <j-search-select-tag placeholder="请选择物流渠道" v-model="queryParam.channelId" dict="hz_logistics_channel,channel_name,id,logistics"></j-search-select-tag>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="渠道描述">
                <a-input placeholder="请输入渠道描述" v-model="queryParam.channelDesc"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.ableStatus" dictCode="logistics_product_data_status"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="产品类型">
                <j-dict-select-tag placeholder="请选择产品类型" v-model="queryParam.productType" dictCode="logistics_product_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="销售价方案">
                <a-input placeholder="请输入销售价方案" v-model="queryParam.salesPriceName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="成本价方案">
                <j-search-select-tag placeholder="请选择成本价方案" v-model="queryParam.costPriceId" dict="hz_logistics_cost_price,name,id,logistics"></j-search-select-tag>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'logisticsProduct:add'">创建物流产品</a-button>
      <a-button type="primary" @click="changeEnable(selectionRows, '2',1)" v-has="'logisticsProduct:enable'">批量启用</a-button>
      <a-button type="primary" @click="changeEnable(selectionRows, '3',1)" v-has="'logisticsProduct:enable'">批量禁用</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('物流产品')">导出</a-button> -->
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!-- <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" :preview="0"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <template slot="costRangeSlot" slot-scope="text, record">
          <div style="text-align: left;" v-if="getCostRangeSlot(text, 0)">公斤费：{{ getCostRangeSlot(text, 0) }}</div>
          <div style="text-align: left;" v-if="getCostRangeSlot(text, 1)">处理费：{{ getCostRangeSlot(text, 1) }}</div>
        </template>

        <template slot="costPriceIdSlot" slot-scope="text, record">
          <div class="costPriceId">
            <span v-if="record.upCostPriceId" class="tag">新重量段</span>
            <span class="text">{{ text }}</span>
          </div>
        </template>

        <template slot="ableStatusSlot" slot-scope="text, record">
          <span :style="{color: getColor(record)}">{{ record.ableStatusText }}</span>
        </template>

        <template slot="channelDescSlot" slot-scope="text, record">
          <j-ellipsis :value="text" :length="20"></j-ellipsis>
        </template>

        <template slot="effectBeginTimeSlot" slot-scope="text, record">
          <div v-if="record.effectBeginTime">开始时间：{{ record.effectBeginTime }}</div>
          <div v-if="record.effectEndTime">结束时间：{{ record.effectEndTime }}</div>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- 已过期不能操作 -->
          <template v-if="record.ableStatus != '4'" >
            <a v-if="record.ableStatus === '2' || record.ableStatus === '3'" v-has="'logisticsProduct:enable'" style="color: red;" @click="changeEnable(record, '3',2) ">禁用</a>
            <a v-if="record.ableStatus === '1' || record.ableStatus === '5'" v-has="'logisticsProduct:enable'" @click="changeEnable(record, '2',2)">启用</a>
            <a-divider type="vertical" v-has="'logisticsProduct:enable'" />
          </template>

          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />

          <a-dropdown>
            <a :class="{'ant-dropdown-link': true, active: record.upCostPriceId ? true : false}">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-has="'logisticsProduct:assignTenant'">
                <a @click="handleAssignCustomers(record)" >分配客户</a>
              </a-menu-item>
              <!-- 仅可编辑未生效的产品 -->
              <a-menu-item v-has="'logisticsProduct:edit'">
                <a @click="handleEdit(record)" >编辑产品</a>
              </a-menu-item>
              <a-menu-item v-has="'logisticsProduct:copy'">
                <a @click="handleReplicationProduct(record)" >复制产品</a>
              </a-menu-item>
              <!-- 草稿状态无此操作 -->
              <template v-if="record.ableStatus != '1'">
                <a-menu-item v-has="'logisticsProduct:changePrice'">
                  <a @click="handlePresetPrice(record)" :class="{active_: record.upCostPriceId ? true : false}" >预设价格</a>
                </a-menu-item>
                <a-menu-item v-has="'logisticsProduct:history'">
                  <a @click="handleHistoricalPrice(record)" >历史价格</a>
                </a-menu-item>
              </template>
              <!-- 操作日志 -->
              <a-menu-item>
                <j-operate-log :id="record.id"></j-operate-log>
              </a-menu-item>
              <!-- <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item> -->
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <logistics-product-modal ref="modalForm" @ok="modalFormOk"></logistics-product-modal>
    <!-- 分配客户 -->
    <logistics-assign-customers-modal ref="modalCustomersForm" @ok="modalFormOk"></logistics-assign-customers-modal>
    <!-- 预设价格 -->
    <logistics-default-price-modal ref="modalDefaultPriceForm" @ok="modalFormOk"></logistics-default-price-modal>
    <!-- 历史价格 -->
    <logistics-historical-price-modal ref="modalHistoryForm" @ok="modalFormOk"></logistics-historical-price-modal>
    <!-- 复制产品 -->
    <logistics-copy-product-modal ref="modalCopyForm" @ok="modalFormOk"></logistics-copy-product-modal>
    <!-- 详情 -->
    <logistics-info-modal ref="modalInfoForm" @ok="modalFormOk"></logistics-info-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import LogisticsProductModal from './modules/LogisticsProductModal'
  import LogisticsAssignCustomersModal from './modules/LogisticsAssignCustomersModal'
  import LogisticsDefaultPriceModal from './modules/LogisticsDefaultPriceModal'
  import LogisticsHistoricalPriceModal from './modules/LogisticsHistoricalPriceModal'
  import LogisticsCopyProductModal from './modules/LogisticsCopyProductModal'
  import LogisticsInfoModal from './modules/LogisticsInfoModal'
  import { getAction, postFormAction } from '@/api/manage'

  export default {
    name: 'LogisticsProductList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      LogisticsProductModal,
      LogisticsAssignCustomersModal,
      LogisticsDefaultPriceModal,
      LogisticsHistoricalPriceModal,
      LogisticsCopyProductModal,
      LogisticsInfoModal
    },
    data () {
      return {
        description: '物流产品管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物流产品名称',
            align:"center",
            dataIndex: 'name',
          },
          {
            title:'物流渠道',
            align:"center",
            dataIndex: 'channelName'
          },
          {
            title:'报价币种',
            align:"center",
            dataIndex: 'currencyType'
          },
          {
            title:'费用范围',
            align:"center",
            dataIndex: 'costRange',
            scopedSlots: { customRender: 'costRangeSlot' }
          },
          {
            title:'产品类型',
            align:"center",
            dataIndex: 'productType_dictText'
          },
          {
            title:'成本价方案',
            align:"center",
            dataIndex: 'costPriceId_dictText',
            scopedSlots: { customRender: 'costPriceIdSlot' }
          },
          {
            title:'销售价方案',
            align:"center",
            dataIndex: 'salesPriceId_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'ableStatus',
            scopedSlots: { customRender: 'ableStatusSlot' }
          },
          {
            title:'渠道描述',
            align:"center",
            dataIndex: 'channelDesc',
            scopedSlots: { customRender: 'channelDescSlot' }
          },
          {
            title:'报价生效时间',
            align:"center",
            dataIndex: 'effectBeginTime',
            scopedSlots: { customRender: 'effectBeginTimeSlot' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/logistics/product/logisticsProduct/list",
          delete: "/logistics/product/logisticsProduct/delete",
          deleteBatch: "/logistics/product/logisticsProduct/deleteBatch",
          exportXlsUrl: "/logistics/product/logisticsProduct/exportXls",
          importExcelUrl: "/logistics/product/logisticsProduct/importExcel",
          changeEnable: "/logistics/product/logisticsProduct/changeEnable"
        },
        dictOptions:{},
        // superFieldList:[],
      }
    },
    created() {
      // this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      // initDictConfig(){
      // },
      // selectRowData(record,index){
      //   const that = this
      //   return {
      //     on:{
      //       dblclick:() => {
      //         // this.handleDetail(record)
      //       }
      //     }
      //   }
      // },
      // getSuperFieldList(){
      //   let fieldList=[];
      //   fieldList.push({type:'string',value:'name',text:'物流产品名称',dictCode:''})
      //   fieldList.push({type:'string',value:'channelId',text:'物流渠道',dictCode:'hz_logistics_channel,channel_name,id'})
      //   fieldList.push({type:'string',value:'costRange',text:'费用范围',dictCode:''})
      //   fieldList.push({type:'string',value:'productType',text:'产品类型',dictCode:'logistics_product_type'})
      //   fieldList.push({type:'string',value:'costPriceId',text:'成本价方案',dictCode:'hz_logistics_cost_price,name,id'})
      //   fieldList.push({type:'string',value:'salesPriceName',text:'销售价方案',dictCode:''})
      //   fieldList.push({type:'string',value:'ableStatus',text:'状态',dictCode:'able_status'})
      //   fieldList.push({type:'date',value:'effectBeginTime',text:'开始时间'})
      //   fieldList.push({type:'date',value:'effectEndTime',text:'结束时间'})
      //   this.superFieldList = fieldList
      // },
      handleAssignCustomers(record){
        this.$refs.modalCustomersForm.edit(record);
        this.$refs.modalCustomersForm.title = "分配客户";
        this.$refs.modalCustomersForm.disableSubmit = false;
      },
      handleEdit(record){
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        // 草稿、未生效状态可编辑
        this.$refs.modalForm.disableSubmit = (record.ableStatus === '1' || record.ableStatus === '2') ? false : true;
      },
      handleReplicationProduct(record){
        this.$refs.modalCopyForm.edit(record);
        this.$refs.modalCopyForm.title = "复制产品";
        this.$refs.modalCopyForm.disableSubmit = false;
      },
      getColor(record){
        if(record.ableStatus === '1'){
          //草稿
          return '#ccc';
        }else if(record.ableStatus === '2'){
          //未生效
          return '#ccc';
        }else if(record.ableStatus === '3'){
          //生效中
          return '#67c23a';
        }else if(record.ableStatus === '4'){
          //已过期
          return 'orange';
        }else if(record.ableStatus === '5'){
          //禁用
          return 'red';
        }
      },
      changeEnable(records, isEnable,type){
        const that = this

        if(!records || (Object.keys(records).length) === 0){
          this.$message.warning('请选择数据!');
          return;
        }

        let productCodes = '';
        if(type === 1){
          //批量操作
          productCodes = records.map(item => item.code).join(",");
        }else if(type === 2){
          //单个操作
          productCodes = records.code;
        }
        if(isEnable === '3'){
          that.$confirm({
            title:'物流产品禁用',
            okText: '继续',
            content: (
              <div>
                <div style="color: red">禁用后物流产品将不可用</div>
                <div>你还要继续吗？</div>
              </div>
            ),
            onOk() {
              that.handleChangeEnable(productCodes, isEnable);
            },
            onCancel() {  },
          });
        }else{
          that.handleChangeEnable(productCodes, isEnable);
        }
      },
      handleChangeEnable(record, isEnable){
        let params = {
          isEnable,
          codes: record
        }
        postFormAction (`${this.url.changeEnable}`, params).then((res) => {
          if(res.success){
            this.$message.success(res.message);
            this.loadData();
            this.selectedRowKeys = [];
          }else{
            this.$message.warning(res.message)
          }
        })
      },
      getCostRangeSlot(text, type){
        return (text && text.split(',') && text.split(',')[type]) || '';
      },
      handlePresetPrice(record){
        this.$refs.modalDefaultPriceForm.edit(record);
        this.$refs.modalDefaultPriceForm.title = "预设价格";
        this.$refs.modalDefaultPriceForm.disableSubmit = false;
      },
      handleHistoricalPrice(record){
        this.$refs.modalHistoryForm.edit(record);
        this.$refs.modalHistoryForm.title = "历史价格";
        this.$refs.modalHistoryForm.disableSubmit = true;
      },
      handleDetail:function(record){
        this.$refs.modalInfoForm.edit(record);
        this.$refs.modalInfoForm.title="详情";
        this.$refs.modalInfoForm.disableSubmit = true;
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  .costPriceId{
    text-align: left;
    position: relative;
    .tag{
      color: #fff;
      background-color: #ff6700;
      border-radius: 4px;
      padding: 1px 2px;
      font-size: 0.6rem;
      position: absolute;
      left: -6px;
      top: -18px;
    }
  }
  .active, .active_{
    position: relative;
    &::after{
      content: '';
      display: block;
      width: 5px;
      height: 5px;
      border-radius: 50%;
      background-color: #ff6700;
      position: absolute;
      top: 0;
      right: 11px;
    }
  }
  .active_{
    &::after{
      top: 8px;
      right: 6px;
    }
  }
</style>